﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Daycare.Models;
using Daycare.Services;

namespace Daycare
{
    public partial class Families : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                PopulateChildrenDropDownList();
                PopulatePreferredHospital();
                PopulateMedicalProfessionals();
                PopulateChildRelationshipDropDownList();
                PopulateDocumentTypeDropDownList();

                ClearChildControls();
                ClearFamilyControls();
            }

            DataTable gvAllergyTable = new DataTable();
            gvAllergyTable.Columns.Add("AllergyName");
            gvAllergyTable.Columns.Add("AllergyNote");

            gvAllergyTable.Rows.Add(new object[] { "TestAllergy", "This is some text.", });
            gvAllergyTable.Rows.Add(new object[] { "TestAllergy", "This is some text.", });
            gvAllergyTable.Rows.Add(new object[] { "TestAllergy", "This is some text.", });
            gvAllergyTable.Rows.Add(new object[] { "TestAllergy", "This is some text.", });
            gvAllergyTable.Rows.Add(new object[] { "TestAllergy", "This is some text. This is some text. This is some text. This is some text. This is some text. This is some text.", });
            gvAllergyTable.Rows.Add(new object[] { "TestAllergy", "This is some text.", });
            gvAllergyTable.Rows.Add(new object[] { "TestAllergy", "This is some text.", });

            gvAllergies.DataSource = gvAllergyTable;
            gvAllergies.DataBind();

            DataTable gvMedicationTable = new DataTable();
            gvMedicationTable.Columns.Add("MedicationName");
            gvMedicationTable.Columns.Add("Reminders");
            gvMedicationTable.Columns.Add("Dosage");

            gvMedicationTable.Rows.Add(new object[] { "TestMedication", "This is some text.", "Reminder stuff" });
            gvMedicationTable.Rows.Add(new object[] { "TestMedication", "This is some text.", "Reminder stuff" });
            gvMedicationTable.Rows.Add(new object[] { "TestMedication", "This is some text.", "Reminder stuff" });
            gvMedicationTable.Rows.Add(new object[] { "TestMedication", "This is some text.", "Reminder stuff" });
            gvMedicationTable.Rows.Add(new object[] { "TestMedication", "This is some text.", "Reminder stuff" });
            gvMedicationTable.Rows.Add(new object[] { "TestMedication", "This is some text.", "Reminder stuff" });
            gvMedicationTable.Rows.Add(new object[] { "TestMedication", "This is some text.", "Reminder stuff" });
            gvMedicationTable.Rows.Add(new object[] { "TestMedication", "This is some text.", "Reminder stuff" });

            gvMedications.DataSource = gvMedicationTable;
            gvMedications.DataBind();

            DataTable gvConditionTable = new DataTable();
            gvConditionTable.Columns.Add("Condition");

            gvConditionTable.Rows.Add(new object[] { "Test condition." });
            gvConditionTable.Rows.Add(new object[] { "Test condition." });
            gvConditionTable.Rows.Add(new object[] { "Test condition." });
            gvConditionTable.Rows.Add(new object[] { "Test condition." });
            gvConditionTable.Rows.Add(new object[] { "Test condition." });
            gvConditionTable.Rows.Add(new object[] { "Test condition." });
            gvConditionTable.Rows.Add(new object[] { "Test condition." });
            gvConditionTable.Rows.Add(new object[] { "Test condition." });

            gvConditions.DataSource = gvConditionTable;
            gvConditions.DataBind();

            DataTable gvChildNoteTable = new DataTable();
            gvChildNoteTable.Columns.Add("Note");
            gvChildNoteTable.Columns.Add("NotedById");
            gvChildNoteTable.Columns.Add("Timestamp");

            gvChildNoteTable.Rows.Add(new object[] { "Test note.", "Jonathan Turner", "12-24-2013 12:47 PM" });
            gvChildNoteTable.Rows.Add(new object[] { "Test note. This is a really long note to see what it looks like. I am going to duplicate this voice. Test note. This is a really long note to see what it looks like. I am going to duplicate this voice.", "Jonathan Turner", "12-24-2013 12:47 PM" });
            gvChildNoteTable.Rows.Add(new object[] { "Test note.", "Jonathan Turner", "12-24-2013 12:47 PM" });
            gvChildNoteTable.Rows.Add(new object[] { "Test note.", "Jonathan Turner", "12-24-2013 12:47 PM" });
            gvChildNoteTable.Rows.Add(new object[] { "Test note.", "Jonathan Turner", "12-24-2013 12:47 PM" });
            gvChildNoteTable.Rows.Add(new object[] { "Test note.", "Jonathan Turner", "12-24-2013 12:47 PM" });
            gvChildNoteTable.Rows.Add(new object[] { "Test note.", "Jonathan Turner", "12-24-2013 12:47 PM" });
            gvChildNoteTable.Rows.Add(new object[] { "Test note.", "Jonathan Turner", "12-24-2013 12:47 PM" });

            gvChildNotes.DataSource = gvChildNoteTable;
            gvChildNotes.DataBind();

            DataTable gvFamilyNoteTable = new DataTable();
            gvFamilyNoteTable.Columns.Add("Note");
            gvFamilyNoteTable.Columns.Add("NotedById");
            gvFamilyNoteTable.Columns.Add("Timestamp");

            gvFamilyNoteTable.Rows.Add(new object[] { "Test note.", "Jonathan Turner", "12-24-2013 12:47 PM" });
            gvFamilyNoteTable.Rows.Add(new object[] { "Test note. This is a really long note to see what it looks like. I am going to duplicate this voice. Test note. This is a really long note to see what it looks like. I am going to duplicate this voice.", "Jonathan Turner", "12-24-2013 12:47 PM" });
            gvFamilyNoteTable.Rows.Add(new object[] { "Test note.", "Jonathan Turner", "12-24-2013 12:47 PM" });
            gvFamilyNoteTable.Rows.Add(new object[] { "Test note.", "Jonathan Turner", "12-24-2013 12:47 PM" });
            gvFamilyNoteTable.Rows.Add(new object[] { "Test note.", "Jonathan Turner", "12-24-2013 12:47 PM" });
            gvFamilyNoteTable.Rows.Add(new object[] { "Test note.", "Jonathan Turner", "12-24-2013 12:47 PM" });
            gvFamilyNoteTable.Rows.Add(new object[] { "Test note.", "Jonathan Turner", "12-24-2013 12:47 PM" });
            gvFamilyNoteTable.Rows.Add(new object[] { "Test note.", "Jonathan Turner", "12-24-2013 12:47 PM" });

            gvFamilyNotes.DataSource = gvFamilyNoteTable;
            gvFamilyNotes.DataBind();

            DataTable gvFamilyDocumentTable = new DataTable();
            gvFamilyDocumentTable.Columns.Add("Name");
            gvFamilyDocumentTable.Columns.Add("Type");
            gvFamilyDocumentTable.Columns.Add("Expiration Date");

            gvFamilyDocumentTable.Rows.Add(new object[] { "Test note.", "Jonathan Turner", "12-24-2013 12:47 PM" });
            gvFamilyDocumentTable.Rows.Add(new object[] { "Test note. This is a really long note to see what it looks like. I am going to duplicate this voice. Test note. This is a really long note to see what it looks like. I am going to duplicate this voice.", "Jonathan Turner", "12-24-2013 12:47 PM" });
            gvFamilyDocumentTable.Rows.Add(new object[] { "Test note.", "Jonathan Turner", "12-24-2013 12:47 PM" });

            gvFamilyDocuments.DataSource = gvFamilyDocumentTable;
            gvFamilyDocuments.DataBind();
        }

        protected void MainPageTab_Click(object sender, EventArgs e)
        {
            LinkButton clickedTab = (LinkButton)sender;

            switch (clickedTab.ID)
            {
                case "lbChildrenTab":
                    pnlChildren.Visible = true;
                    pnlContacts.Visible = false;
                    pnlFamilyDetails.Visible = false;

                    lbChildrenTab.CssClass = "tab selected";
                    lbContactsTab.CssClass = "tab";
                    lbFamilyDetailsTab.CssClass = "tab";

                    break;
                case "lbContactsTab":
                    pnlChildren.Visible = false;
                    pnlContacts.Visible = true;
                    pnlFamilyDetails.Visible = false;

                    lbChildrenTab.CssClass = "tab";
                    lbContactsTab.CssClass = "tab selected";
                    lbFamilyDetailsTab.CssClass = "tab";

                    break;
                case "lbFamilyDetailsTab":
                    pnlChildren.Visible = false;
                    pnlContacts.Visible = false;
                    pnlFamilyDetails.Visible = true;

                    lbChildrenTab.CssClass = "tab";
                    lbContactsTab.CssClass = "tab";
                    lbFamilyDetailsTab.CssClass = "tab selected";

                    break;
            }
        }

        protected void ChildOptionsTab_Click(object sender, EventArgs e)
        {
            LinkButton clickedTab = (LinkButton)sender;

            switch (clickedTab.ID)
            {
                case "lbAllergiesTab":
                    pnlAllergies.Visible = true;
                    pnlMedications.Visible = false;
                    pnlConditions.Visible = false;
                    pnlInsurance.Visible = false;

                    lbAllergiesTab.CssClass = "tab selected";
                    lbMedicationsTab.CssClass = "tab";
                    lbConditionsTab.CssClass = "tab";
                    lbInsuranceTab.CssClass = "tab";

                    break;
                case "lbMedicationsTab":
                    pnlAllergies.Visible = false;
                    pnlMedications.Visible = true;
                    pnlConditions.Visible = false;
                    pnlInsurance.Visible = false;

                    lbAllergiesTab.CssClass = "tab";
                    lbMedicationsTab.CssClass = "tab selected";
                    lbConditionsTab.CssClass = "tab";
                    lbInsuranceTab.CssClass = "tab";

                    break;
                case "lbConditionsTab":
                    pnlAllergies.Visible = false;
                    pnlMedications.Visible = false;
                    pnlConditions.Visible = true;
                    pnlInsurance.Visible = false;

                    lbInsuranceTab.CssClass = "tab";
                    lbAllergiesTab.CssClass = "tab";
                    lbMedicationsTab.CssClass = "tab";
                    lbConditionsTab.CssClass = "tab selected";

                    break;
                case "lbInsuranceTab":
                    pnlAllergies.Visible = false;
                    pnlMedications.Visible = false;
                    pnlConditions.Visible = false;
                    pnlInsurance.Visible = true;

                    lbInsuranceTab.CssClass = "tab selected";
                    lbAllergiesTab.CssClass = "tab";
                    lbMedicationsTab.CssClass = "tab";
                    lbConditionsTab.CssClass = "tab";

                    break;
                case "lbMedicalTab":
                    pnlMedical.Visible = true;
                    pnlDental.Visible = false;

                    lbMedicalTab.CssClass = "tab selected";
                    lbDentalTab.CssClass = "tab";

                    break;
                case "lbDentalTab":
                    pnlMedical.Visible = false;
                    pnlDental.Visible = true;

                    lbMedicalTab.CssClass = "tab";
                    lbDentalTab.CssClass = "tab selected";

                    break;
            }

            lbAddUpdateChildOptions.Visible = (lbInsuranceTab.CssClass != "tab selected");
        }

        protected void ContactsTab_Click(object sender, EventArgs e)
        {
            LinkButton clickedTab = (LinkButton)sender;

            switch (clickedTab.ID)
            {
                case "lbPrimaryContactTab":
                    lbPrimaryContactTab.CssClass = "tab selected";
                    lbSecondaryContactTab.CssClass = "tab";
                    lbAdditionalContactsTab.CssClass = "tab";
                    lbNoContactTab.CssClass = "tab";
                    PopulateContactControls("Primary");

                    break;
                case "lbSecondaryContactTab":
                    lbPrimaryContactTab.CssClass = "tab";
                    lbSecondaryContactTab.CssClass = "tab selected";
                    lbAdditionalContactsTab.CssClass = "tab";
                    lbNoContactTab.CssClass = "tab";
                    PopulateContactControls("Secondary");

                    break;
                case "lbAdditionalContactsTab":
                    lbPrimaryContactTab.CssClass = "tab";
                    lbSecondaryContactTab.CssClass = "tab";
                    lbAdditionalContactsTab.CssClass = "tab selected";
                    lbNoContactTab.CssClass = "tab";
                    PopulateContactControls("Additional");

                    break;
                case "lbNoContactTab":
                    lbPrimaryContactTab.CssClass = "tab";
                    lbSecondaryContactTab.CssClass = "tab";
                    lbAdditionalContactsTab.CssClass = "tab";
                    lbNoContactTab.CssClass = "tab selected ";
                    PopulateContactControls("No Contact");

                    break;
                case "lbMailingAddress":
                    pnlMailingAddress.Visible = true;
                    pnlBillingAddress.Visible = false;

                    lbBillingAddress.CssClass = "tab";
                    lbMailingAddress.CssClass = "tab selected";

                    break;
                case "lbBillingAddress":
                    pnlMailingAddress.Visible = false;
                    pnlBillingAddress.Visible = true;

                    lbBillingAddress.CssClass = "tab selected";
                    lbMailingAddress.CssClass = "tab";

                    break;
            }
        }

        protected void ddlChildren_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearChildControls();
            ClearFamilyControls();

            if (ddlChildren.SelectedIndex > 0)
            {
                int selectedChildId = Int32.Parse(ddlChildren.SelectedValue);

                PopulateFamilyChildrenListBox(selectedChildId);
                PopulateContactControls("Primary");
            }
        }

        protected void PopulateChildrenDropDownList()
        {
            FamilyService fs = new FamilyService();
            ddlChildren.AppendDataBoundItems = true;
            ddlChildren.Items.Clear();
            ddlChildren.Items.Add(new ListItem("Add Family", "0"));
            ddlChildren.DataTextField = "FullName";
            ddlChildren.DataValueField = "ChildId";
            ddlChildren.DataSource = fs.GetChildren(cbIncludeInactiveFamilies.Checked);
            ddlChildren.DataBind();
        }

        protected void PopulateFamilyChildrenListBox(int selectedChildId)
        {
            lbChildren.AppendDataBoundItems = true;
            lbChildren.Items.Clear();
            lbChildren.Items.Add(new ListItem("Add Child", "0"));
            lbChildren.DataTextField = "Fullname";
            lbChildren.DataValueField = "ChildId";

            FamilyService fs = new FamilyService();
            if (selectedChildId > 0)
            {
                lbChildren.DataSource = fs.GetFamilyChildren(int.Parse(ddlChildren.SelectedValue));
            }
            else
            {
                lbChildren.DataSource = new DataTable();
            }

            lbChildren.DataBind();
        }

        protected void LoadChildControls(int selectedChildId)
        {

        }

        protected void LoadContactControls(int selectedContactId)
        {

        }

        protected void LoadFamilyControls()
        {


            ContactsTab_Click(lbPrimaryContactTab, new EventArgs());
        }

        protected void PopulateContactControls(String selectedContactTab)
        {
            switch (selectedContactTab)
            {
                case "Primary":
                    pnlAdditionalContacts.Visible = false;
                    //PopulateAddressInformation()

                    break;
                case "Secondary":
                    pnlAdditionalContacts.Visible = false;
                    //PopulateAddressInformation()

                    break;
                case "Additional":
                    pnlAdditionalContacts.Visible = true;

                    PopulateAdditionalContactsListBox(Int32.Parse(ddlChildren.SelectedValue));

                    break;
                case "No Contact":
                    pnlAdditionalContacts.Visible = true;
                    PopulateAdditionalContactsListBox(Int32.Parse(ddlChildren.SelectedValue));

                    break;
            }
        }

        protected void PopulateAddressInformation(int selectedContactId)
        {
            if (selectedContactId > 0)
            {

            }
            else
            {
                ClearAddressForm();
            }
        }

        protected void ClearChildControls()
        {
            int selectedChildId = Int32.Parse(ddlChildren.SelectedValue);

            PopulateChildrenListBox(selectedChildId);
            iChildPhoto.ImageUrl = Configuration.DefaultImageFullPath;
            ClearChildForm();

            PopulateChildGridViews(selectedChildId);

            ddlPreferredHospital.SelectedIndex = 0;
            ddlChildDentist.SelectedIndex = 0;
            ddlChildDoctor.SelectedIndex = 0;

            tbMedicalInsuranceProvider.Text = String.Empty;
            tbDentalInsuranceProvider.Text = String.Empty;

            ToggleAddChildButton(selectedChildId);

            cbActiveChild.Checked = true;

            ChildOptionsTab_Click(lbAllergiesTab, new EventArgs());
        }

        protected void ClearFamilyControls()
        {
            int selectedChildId = Int32.Parse(ddlChildren.SelectedValue);

            PopulateAdditionalContactsListBox(selectedChildId);
            iContactPhoto.ImageUrl = Configuration.DefaultImageFullPath;

            ToggleAddContactButton(selectedChildId);
            ClearContactForm();
            ClearAddressForm();
            ClearDocumentForm();
            ClearFamilyNotes();

            ContactsTab_Click(lbPrimaryContactTab, new EventArgs());
        }

        protected void ClearChildForm()
        {
            tbChildFirstName.Text = String.Empty;
            tbChildMiddleName.Text = String.Empty;
            tbChildLastName.Text = String.Empty;
            tbChildNickName.Text = String.Empty;
            tbChildDateOfBirth.Text = String.Empty;
            tbChildAgeGroup.Text = String.Empty;
        }

        protected void ClearContactForm()
        {
            tbContactFirstName.Text = String.Empty;
            tbContactMiddleName.Text = String.Empty;
            tbContactLastName.Text = String.Empty;
            tbContactEmailAddress.Text = String.Empty;
            tbContactHomePhoneNumber.Text = String.Empty;
            tbContactWorkPhoneNumber.Text = String.Empty;
            tbContactCellPhoneNumber.Text = String.Empty;

            ddlRelationship.SelectedIndex = 0;

            cbEmergencyContact.Checked = false;
            cbApprovedForPickup.Checked = false;
        }

        protected void ClearDocumentForm()
        {
            tbDocumentName.Text = String.Empty;
            tbDocumentExpirationDate.Text = String.Empty;

            ddlDocumentTypes.SelectedIndex = 0;
        }

        protected void ClearFamilyNotes()
        {
            PopulateFamilyNotes(Int32.Parse(ddlChildren.SelectedValue));
        }

        protected void PopulateFamilyNotes(int selectedChildId)
        {
            FamilyService fs = new FamilyService();
            if (selectedChildId > 0)
            {
                gvFamilyNotes.DataSource = fs.GetFamilyNotes(int.Parse(ddlChildren.SelectedValue));
            }
            else
            {
                gvFamilyNotes.DataSource = new DataTable();
            }

            gvFamilyNotes.DataBind();
        }

        protected void ClearAddressForm()
        {
            tbMailingAttentionTo.Text = String.Empty;
            tbMailingCity.Text = String.Empty;
            tbMailingState.Text = String.Empty;
            tbMailingStreetAddressOne.Text = String.Empty;
            tbMailingStreetAddressTwo.Text = String.Empty;
            tbMailingZipCode.Text = String.Empty;

            tbBillingAddressTo.Text = String.Empty;
            tbBillingCity.Text = String.Empty;
            tbBillingState.Text = String.Empty;
            tbBillingStreetAddressOne.Text = String.Empty;
            tbBillingStreetAddressTwo.Text = String.Empty;
            tbBillingZipCode.Text = String.Empty;
        }

        protected void PopulateChildRelationshipDropDownList()
        {
            ddlRelationship.AppendDataBoundItems = true;
            ddlRelationship.Items.Clear();
            ddlRelationship.Items.Add(new ListItem("Select One", "0"));
            ddlRelationship.DataTextField = "ChildRelationshipName";
            ddlRelationship.DataValueField = "ChildRelationshipId";
            //ddlRelationship.DataSource =
            ddlRelationship.DataBind();
        }

        protected void PopulateAdditionalContactsListBox(int selectedChildId)
        {
            lbAdditionalContacts.AppendDataBoundItems = true;
            lbAdditionalContacts.Items.Clear();
            lbAdditionalContacts.Items.Add(new ListItem("Add Contact", "0"));
            lbAdditionalContacts.DataTextField = "ChildContactFullName";
            lbAdditionalContacts.DataValueField = "ChildContactId";

            if (selectedChildId > 0)
            {
                //lbAdditionalContacts.DataSouce = 
            }
            else
            {
                lbAdditionalContacts.DataSource = new DataTable();
            }

            lbAdditionalContacts.DataBind();
        }

        protected void ToggleAddChildButton(int selectedChildId)
        {
            if (selectedChildId > 0)
            {
                bAddUpdateChild.Text = "Update Child";
                bAddUpdateChild.CommandName = "Update";
            }
            else
            {
                bAddUpdateChild.Text = "Add Child";
                bAddUpdateChild.CommandName = "Add";
            }
        }

        protected void bAddUpdateChildClick(object sender, EventArgs e)
        {
            ChildService cs = new ChildService();
            ImageService ims = new ImageService();

            string fn = ims.UploadImage(fuContactPhoto.FileName, string.Format("child-{0}", tbChildFirstName.Text + ' ' + tbChildLastName.Text));
            ims.AddImage(fn);

            Child c = new Child
            {
                childId = lbChildren.SelectedValue == string.Empty ? 0 : int.Parse(lbChildren.SelectedValue),
                dateOfBirth = DateTime.Parse(tbChildDateOfBirth.Text).Date,
                firstName = tbChildFirstName.Text,
                middleName = tbChildMiddleName.Text,
                nickName = tbChildNickName.Text,
                lastName = tbChildLastName.Text
            };
            if (c.childId == 0)
            {
                c.isActive = true;
                cs.insertChild(c);
            }
            else
            {
                c.isActive = cbActiveChild.Checked;
                cs.updateChild(c);
            }

        }

        protected void ToggleAddContactButton(int selectedChildId)
        {
            if (selectedChildId > 0)
            {
                bAddUpdateContact.Text = "Update Contact";
                bAddUpdateContact.CommandName = "Update";
            }
            else
            {
                bAddUpdateContact.Text = "Add Contact";
                bAddUpdateContact.CommandName = "Add";
            }
        }

        protected void PopulateChildrenListBox(int selectedChildId)
        {
            lbChildren.AppendDataBoundItems = true;
            lbChildren.Items.Clear();
            lbChildren.Items.Add(new ListItem("Add Child", "0"));
            lbChildren.DataTextField = "DocumentTypeName";
            lbChildren.DataValueField = "DocumentTypeId";

            if (selectedChildId > 0)
            {
                //lbChildren.DataSource = 

            }

            lbChildren.DataBind();
        }

        protected void PopulateChildGridViews(int selectedChildId)
        {
            if (selectedChildId > 0)
            {
                //gvAllergies.DataSource = 
                //gvConditions.DataSource =
                //gvMedications.DataSource =
                //gvChildNotes.DataSource =
            }
            else
            {
                gvAllergies.DataSource = new DataTable();
                gvConditions.DataSource = new DataTable();
                gvMedications.DataSource = new DataTable();
                gvChildNotes.DataSource = new DataTable();
            }

            gvAllergies.DataBind();
            gvConditions.DataBind();
            gvMedications.DataBind();
            gvChildNotes.DataBind();
        }

        protected void PopulatePreferredHospital()
        {
            ddlPreferredHospital.AppendDataBoundItems = true;
            ddlPreferredHospital.Items.Clear();
            ddlPreferredHospital.Items.Add(new ListItem("Select One", "0"));
            ddlPreferredHospital.DataTextField = "HospitalName";
            ddlPreferredHospital.DataValueField = "HospitalId";
            //ddlPreferredHospital.DataSource = 
            ddlPreferredHospital.DataBind();
        }

        protected void PopulateMedicalProfessionals()
        {
            ddlChildDoctor.AppendDataBoundItems = true;
            ddlChildDoctor.Items.Clear();
            ddlChildDoctor.Items.Add(new ListItem("Select One", "0"));
            ddlChildDoctor.DataTextField = "MedicalProfessionalFullName";
            ddlChildDoctor.DataValueField = "MedicalProfessionalId";
            //ddlChildDoctor.DataSource = 
            ddlChildDoctor.DataBind();

            ddlChildDentist.AppendDataBoundItems = true;
            ddlChildDentist.Items.Clear();
            ddlChildDentist.Items.Add(new ListItem("Select One", "0"));
            ddlChildDentist.DataTextField = "MedicalProfessionalFullName";
            ddlChildDentist.DataValueField = "MedicalProfessionalId";
            //ddlChildDentist.DataSource = 
            ddlChildDentist.DataBind();
        }

        protected void PopulateDocumentTypeDropDownList()
        {
            ddlDocumentTypes.AppendDataBoundItems = true;
            ddlDocumentTypes.Items.Clear();
            ddlDocumentTypes.Items.Add(new ListItem("Select One", "0"));
            ddlDocumentTypes.DataTextField = "DocumentTypeName";
            ddlDocumentTypes.DataValueField = "DocumentTypeId";
            //ddlDocumentTypes.DataSource = 
            ddlDocumentTypes.DataBind();
        }

        protected void lbChildren_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox lbChildren = (ListBox)sender;

            if (lbChildren.SelectedItem != null)
            {
                if (lbChildren.SelectedIndex == 0)
                {
                    ClearChildControls();
                }
                else
                {
                    LoadChildControls(Int32.Parse(lbChildren.SelectedValue));
                }
            }
        }

        protected void lbAdditionalContacts_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBox lbAdditionalContacts = (ListBox)sender;

            if (lbAdditionalContacts.SelectedItem != null)
            {
                if (lbAdditionalContacts.SelectedIndex == 0)
                {
                    ClearContactForm();
                }
                else
                {
                    LoadContactControls(Int32.Parse(lbAdditionalContacts.SelectedValue));
                }
            }
        }

        protected void ddlDocumentTypeFilter_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void lbDocumentLink_Click(object sender, EventArgs e)
        {
            String filePath = Configuration.DocumentPath + lbDocumentLink.Text;
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + System.IO.Path.GetFileName(filePath));
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.WriteFile(filePath);
            HttpContext.Current.Response.End();
        }

        protected void DisplayAddUpdatePopup_Click(object sender, EventArgs e)
        {
            LinkButton clickedListBox = (LinkButton)sender;

            switch (clickedListBox.ClientID)
            {
                case "lbAddUpdateChildOptions":
                //fall through
                case "lbEditChildOption":
                //fall through
                case "lbDeleteChildOption":
                    if (lbAllergiesTab.CssClass == "tab selected")
                    {
                        if (clickedListBox.ClientID == "lbEditChildOption")
                        {
                            lblAddUpdateAllergy.Text = "Edit an allergy.";
                            ddlAllergies.Visible = true;
                            tbAllergyNote.Visible = true;
                            lbAllergyConfig.Visible = true;
                            lblAllergyNoteLabel.Visible = true;
                        }
                        else if (clickedListBox.ClientID == "lbDeleteChildOption")
                        {
                            lblAddUpdateAllergy.Text = "Delete child allergy?";
                            ddlAllergies.Visible = false;
                            tbAllergyNote.Visible = false;
                            lblAllergyNoteLabel.Visible = false;
                            lbAllergyConfig.Visible = false;
                        }
                        else
                        {
                            ddlAllergies.Visible = true;
                            tbAllergyNote.Visible = true;
                            lblAllergyNoteLabel.Visible = true;
                            lblAddUpdateAllergy.Text = "Add an allergy.";
                            lbAllergyConfig.Visible = true;
                        }

                        mpeAllergies.Show();
                    }
                    else if (lbMedicationsTab.CssClass == "tab selected")
                    {
                        if (clickedListBox.ClientID == "lbEditChildOption")
                        {
                            lblAddUpdateMedication.Text = "Edit a Medication";

                            pnlAddEditMedications.Visible = true;
                        }
                        else if (clickedListBox.ClientID == "lbDeleteChildOption")
                        {
                            lblAddUpdateMedication.Text = "Delete a child medication.";
                            pnlAddEditMedications.Visible = false;
                        }
                        else
                        {
                            lblAddUpdateMedication.Text = "Add a Medication";
                            pnlAddEditMedications.Visible = true;
                        }

                        mpeMedications.Show();
                    }
                    else if (lbConditionsTab.CssClass == "tab selected")
                    {
                        if (clickedListBox.ClientID == "lbEditChildOption")
                        {
                            lblAddCondition.Text = "Edit a Condition";

                            pnlAddUpdateCondition.Visible = true;
                        }
                        else if (clickedListBox.ClientID == "lbDeleteChildOption")
                        {
                            lblAddCondition.Text = "Delete a child condition.";
                            pnlAddUpdateCondition.Visible = false;
                        }
                        else
                        {
                            lblAddCondition.Text = "Add a Condition";
                            pnlAddUpdateCondition.Visible = true;
                        }


                        mpeConditions.Show();
                    }

                    break;
                case "lbAddUpdateChildNote":
                    mpeChildNotes.Show();
                    break;
                case "lbAddUpdateFamilyNotes":
                    mpeFamilyNotes.Show();
                    break;
                case "lbAddUpdateFamilyDocuments":
                case "lbEditFamilyOption":
                case "lbDeleteFamilyOption":
                    if (clickedListBox.ClientID == "lbEditFamilyOption")
                    {
                        lblAddUpdateFamilyDocument.Text = "Edit a Document";

                        pnlAddUpdateFamilyDocument.Visible = true;

                        fuDocument.Enabled = false;
                    }
                    else if (clickedListBox.ClientID == "lbDeleteFamilyOption")
                    {
                        lblAddUpdateFamilyDocument.Text = "Delete a family document.";
                        pnlAddUpdateFamilyDocument.Visible = false;
                    }
                    else
                    {
                        lblAddUpdateFamilyDocument.Text = "Add a Document";
                        pnlAddUpdateFamilyDocument.Visible = true;

                        fuDocument.Enabled = true;
                    }

                    mpeFamilyDocuments.Show();
                    break;
            }
        }

        protected void rbChildAllergiesSelect_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}